package com.one.apartmentsystem.app.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.one.apartmentsystem.app.context.BaseContext;
import com.one.apartmentsystem.app.domain.HousesInfo;
import com.one.apartmentsystem.app.domain.PaymentManage;
import com.one.apartmentsystem.app.domain.PropertyItem;
import com.one.apartmentsystem.app.dto.PageDTO;
import com.one.apartmentsystem.app.mapper.PaymentManageMapper;
import com.one.apartmentsystem.app.mapper.PaymentRecordMapper;
import com.one.apartmentsystem.app.mapper.PropertyItemMapper;
import com.one.apartmentsystem.app.service.HousesInfoService;
import com.one.apartmentsystem.app.service.PaymentManageService;
import com.one.apartmentsystem.app.service.PaymentRecordService;
import com.one.apartmentsystem.app.vo.HouseDetailsVO;
import com.one.apartmentsystem.app.vo.PageResultVO;
import com.one.apartmentsystem.app.vo.PaymentDetailsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author mpk16
 * @description 针对表【payment_manage】的数据库操作Service实现
 * @createDate 2024-08-03 13:39:49
 */
@Service
public class PaymentManageServiceImpl extends ServiceImpl<PaymentManageMapper, PaymentManage>
        implements PaymentManageService {
    @Autowired
    PaymentManageMapper paymentManageMapper;
    @Autowired
    PaymentRecordMapper paymentRecordMapper;
    @Autowired
    PropertyItemMapper propertyItemMapper;
    @Autowired
    HousesInfoService housesInfoService;



    @Override
    public PageResultVO<PaymentDetailsVO> unpaid() {

        PageResultVO<PaymentDetailsVO> pageResultVO = new PageResultVO<>();
        List<HouseDetailsVO> houseDetails = housesInfoService.getHouseByUserId(BaseContext.getCurrentId());
        if (houseDetails == null) {
            return pageResultVO;
        }
        List<PaymentDetailsVO> paymentDetails =new ArrayList<>();
        houseDetails.forEach(
                houseDetailsVO -> {
                    List<PaymentDetailsVO> detailsVOList = paymentManageMapper.unpaid(houseDetailsVO.getHousesId());
                    paymentDetails.addAll(detailsVOList.stream().map(
                            paymentDetailsVO -> {
                                paymentDetailsVO.setImages(houseDetailsVO.getImage());
                                paymentDetailsVO.setContractId(houseDetailsVO.getContractId());
                                return paymentDetailsVO;
                            }
                    ).collect(Collectors.toList()));
                }

        );

        pageResultVO.setList(paymentDetails);
        pageResultVO.setTotal(paymentDetails.size());

        return pageResultVO;

    }

    @Override
    public PageResultVO<PaymentDetailsVO> paid() {
        PageResultVO<PaymentDetailsVO> pageResultVO = new PageResultVO<>();
        List<HouseDetailsVO> houseDetails = housesInfoService.getHouseByUserId(BaseContext.getCurrentId());
        if (houseDetails == null) {
            return pageResultVO;
        }
        List<PaymentDetailsVO> paymentDetails =new ArrayList<>();
        houseDetails.forEach(
                houseDetailsVO -> {
                    List<PaymentDetailsVO> detailsVOList = paymentManageMapper.paid(houseDetailsVO.getHousesId());
                    paymentDetails.addAll(detailsVOList.stream().map(
                            paymentDetailsVO -> {
                                paymentDetailsVO.setImages(houseDetailsVO.getImage());
                                paymentDetailsVO.setContractId(houseDetailsVO.getContractId());
                                return paymentDetailsVO;
                            }
                    ).collect(Collectors.toList()));
                }

        );

        pageResultVO.setList(paymentDetails);
        pageResultVO.setTotal(paymentDetails.size());

        return pageResultVO;
    }
}




